package com.bugpool.leilema.freamwork.mybatis;

import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wh
 * @Description
 * @Date 2023/6/13 11:25
 * @Version 1.0
 **/
@Configuration
@AutoConfigureAfter(PaginationInnerInterceptor.class)
@MapperScan("com.bugpool.leilema.*.mapper")
public class MybatisInterceptorAutoConfiguration implements InitializingBean {

    @Resource
    private List<SqlSessionFactory> sqlSessionFactoryList;


    @Override
    public void afterPropertiesSet() throws Exception {
        //创建自定义mybatis拦截器，添加到chain的最后面
        MybatisDataPermissionIntercept mybatisInterceptor = new MybatisDataPermissionIntercept();

        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
            //自己添加
            configuration.addInterceptor(mybatisInterceptor);
        }
    }
}
